package com.yupi.cli.example;

import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;

@Command(name = "Login", version = "Login 1.0", mixinStandardHelpOptions = true)
public class Login implements Callable<Integer> {
    @CommandLine.Option(names = {"-u", "-user"}, description = "User name", echo = true)
    String user;

    @CommandLine.Option(names = {"-p", "-password"}, description = "Password", interactive = true, arity = "0..1")
    String password;

    @CommandLine.Option(names = {"-cp", "-checkPassword"}, description = "Check Password", interactive = true, arity = "0..1")
    String checkPassword;

    @Override
    public Integer call() throws Exception {
        System.out.println("password:" + password);
        System.out.println("checkPassword:" + checkPassword);
        return 0;
    }

    public static void main(String[] args) {
        //通过反射判断是否包含命令行参数
//        List<String> newArgs = new ArrayList<>(Arrays.stream(args).toList());
//        try {
//            Class<?> clazz = Class.forName("com.yupi.cli.example.Login");
//            Field[] fields = clazz.getDeclaredFields();
//            for (Field field : fields) {
//                CommandLine.Option option = field.getAnnotation(CommandLine.Option.class);
//                String[] list = option.names();
//                boolean isExist = false;
//                for (String name : list) {
//                    for (String arg : args) {
//                        if (name.equals(arg)) {
//                            isExist = true;
//                            break;
//                        }
//                    }
//                }
//                if (!isExist) newArgs.add(list[0]);
//            }
//            System.out.println(newArgs);
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }
//        System.out.println();
        new CommandLine(new Login()).execute(args);
    }
}
